package com.cea.papyrus.diagram.classdiagram.figures;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Rectangle;

import com.cea.papyrus.core.figures.NodeFigure;
import com.cea.papyrus.core.figures.RoundedRectangleNodeFigure;

public class OpaqueActionFigure extends RoundedRectangleNodeFigure {

	/** The action name offset. */
	protected int actionNameOffset;
	
	/** The action name location delta. */
	protected int actionNameLocationDelta;

	protected OpaqueActionLabelFigure labelFigure;
	

	public OpaqueActionFigure () {
		super();
		labelFigure = new OpaqueActionLabelFigure();
		add(labelFigure);
	}

	protected class OpaqueActionLabelFigure extends NodeFigure {

		/**
		 * The Constructor.
		 */
		public OpaqueActionLabelFigure() {
			super();
			ToolbarLayout layout = new ToolbarLayout();
			setLayoutManager(layout);
			layout.setSpacing(2);
		}

		/**
		 * Paint border.
		 * 
		 * @param graphics the graphics
		 */
		@Override
		public void paintBorder(Graphics graphics) {}

		/**
		 * Sets the bounds.
		 * 
		 * @param rect the rect
		 */
		@Override
		public void setBounds(Rectangle rect) {
			int labelHeight = 20;
			int labelWidth = getNameLabel().getPreferredSize().width + 4;

			// Calculate height
			if (getStereotypesLabel() != null) {
				labelHeight = labelHeight+ getStereotypesLabel().getPreferredSize().height;
			}
			if (getQualifiedNameLabel() != null) {
				labelHeight = labelHeight+ getQualifiedNameLabel().getPreferredSize().height;
			}
			if (getIconLabel() != null) {
				if (getIconLabel().getIcon() != null){
					labelHeight = labelHeight+ getIconLabel().getPreferredSize().height;
				}				
			}
			// Dimension of the rounded rectangle
			int fatherHeight = getParent().getSize().height;
			int fatherWidth = getParent().getSize().width;

			if (labelWidth > (fatherWidth - actionNameOffset)){
				labelWidth = fatherWidth - actionNameOffset;			
			}

			Rectangle labelRect = new Rectangle();
			labelRect.setLocation((fatherWidth - labelWidth - actionNameOffset) / 2 + actionNameLocationDelta, (fatherHeight - labelHeight) / 2);
			labelRect.setSize(labelWidth, labelHeight);

			super.setBounds(labelRect);
		}
	}

}
